package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.PalletBean;
import com.sanyang.pda.bean.request.PutOnRequest;
import com.sanyang.pda.contract.PutOnContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class PutOnPresenter extends BasePresenter<PutOnContract.View> implements PutOnContract.Presenter {

    @Override
    public void palletizingInfo(String pallet,String entryNo) {
        NetUtils.geApi().palletizingInfo(pallet,entryNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<PalletBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<PalletBean>> palletBeanBaseAppEntity) throws Exception {
                        getView().setPallet(palletBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void reclaimingPutOnShelves(PutOnRequest putOnRequest) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(putOnRequest));
        NetUtils.geApi().reclaimingPutOnShelves(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<PalletBean>>() {
                    @Override
                    public void accept(BaseAppEntity<PalletBean> palletBeanBaseAppEntity) throws Exception {
                        getView().putSuccess();
                        getView().showMessage(palletBeanBaseAppEntity.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void putOnShelves(PutOnRequest putOnRequest) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(putOnRequest));
        NetUtils.geApi().putOnShelves(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<PalletBean>>() {
                    @Override
                    public void accept(BaseAppEntity<PalletBean> palletBeanBaseAppEntity) throws Exception {
                        getView().putSuccess();
                        getView().showMessage(palletBeanBaseAppEntity.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void selectArticleInfoByBarcode(String barcode) {
        NetUtils.geApi().selectArticleInfoByBarcode(barcode)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ByBarcodeBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ByBarcodeBean> byBarcodeBeanBaseAppEntity) throws Exception {
                       // getView().setGoods(byBarcodeBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
